package com.ruoyi.flow.common.tools.mediaconverter.converter.converter;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.Set;

import com.ruoyi.flow.common.tools.mediaconverter.converter.reflections.Reflections;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.collect.HashBasedTable;
import com.google.common.collect.Table;

public class ConverterFactory {
	private static final Logger LOGGER = LoggerFactory.getLogger(ConverterFactory.class);
	private Table<String, String, Class<?>> converterStore = HashBasedTable.<String, String, Class<?>>create();
	private static volatile ConverterFactory factory = null;

	private ConverterFactory() {
		Reflections reflections = new Reflections(ConverterFactory.class.getPackage().getName());
		Set<Class<?>> classes = reflections.getTypesAnnotatedWith(Converter.class);
		if (classes.size() > 0) {
			for (Class<?> clazz : classes) {
				Converter converter = clazz.getAnnotation(Converter.class);
				String srcFormat = converter.srcFormat();
				String destFormat = converter.destFormat();
				converterStore.put(srcFormat, destFormat, clazz);
			}
		}
	}

	public static ConverterFactory getFactory() {
		if (factory == null) {
			synchronized (ConverterFactory.class) {
				if (factory == null) {
					factory = new ConverterFactory();
				}
			}
		}
		return factory;
	}

	/**
	 * 获取转换器
	 * 
	 * @param srcFilePath
	 * @param destFilePath
	 * @return
	 */
	public IConverter getConverter(String srcFilePath, String destFilePath) {
		String srcFormat = StringUtils.substringAfterLast(srcFilePath, ".");
		srcFormat = StringUtils.lowerCase(srcFormat);
		String destFormat = StringUtils.substringAfterLast(destFilePath, ".");
		destFormat = StringUtils.lowerCase(destFormat);
        
		Class<?> clazz = converterStore.get(srcFormat, destFormat);
		if (clazz != null) {
			try {
				Constructor cons = clazz.getDeclaredConstructor(String.class, String.class);
				IConverter converter = (IConverter) cons.newInstance(srcFilePath, destFilePath);
				return converter;
			} catch (NoSuchMethodException | SecurityException | InstantiationException | IllegalAccessException
					| IllegalArgumentException | InvocationTargetException e) {
				e.printStackTrace();
			}
		}else {
			LOGGER.warn("can't find the converter for srcFormat:{},destFormat:{}",srcFormat,destFormat);
		}
		return null;
	}

	public static void main(String[] args) {
		System.out.println(ConverterFactory.class.getPackage());
		ConverterFactory factory = ConverterFactory.getFactory();
		IConverter silk2mp3converter = factory.getConverter("D:\\silk2mp3\\wxid_y2prj0189ws122.silk", "D:\\silk2mp3\\wxid_y2prj0189ws122.mp3");
		//converter.invoke("D:\\\\silk2mp3", null);
		System.out.println(silk2mp3converter.getClass());
		IConverter amr2mp3converter = factory.getConverter("D:\\silk2mp3\\wxid_y2prj0189ws122.amr", "D:\\silk2mp3\\wxid_y2prj0189ws122.mp3");
		System.out.println(amr2mp3converter.getClass());
	}
}
